package com.zzyl.nursing.controller.member;

import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.service.IReservationService;
import com.zzyl.nursing.vo.TimeCountVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 预约信息Controller
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/member/reservation")
@Api(tags = "预约信息相关接口")
public class MemberReservationController extends BaseController {

    @Autowired
    private IReservationService reservationService;

    /**
     * 查询取消预约数量
     *
     * @return
     */
    @GetMapping("/cancelled-count")
    @ApiOperation("查询取消预约数量")
    public R<Integer> getCancelledReservationCount() {
        Long userId = UserThreadLocal.getUserId();
        int count = reservationService.getCancelledReservationCount(userId);
        return R.ok(count);
    }

    /**
     * 查询每个小时段剩余预约次数
     *
     * @param time
     * @return
     */
    @GetMapping("/countByTime")
    public R<List<TimeCountVo>> countReservationsForEachTimeWithinTimeRange(Long time) {
        List<TimeCountVo> list = reservationService.countReservationsForTime(time);
        return R.ok(list);
    }

    /**
     * 新增预约信息
     *
     * @param reservation
     * @return
     */
    @PostMapping
    public AjaxResult addReservation(@RequestBody Reservation reservation) {
        return toAjax(reservationService.insertReservation(reservation));
    }

    /**
     * 分页查询预约信息
     *
     * @param
     * @return
     */
    @GetMapping("/page")
    public R<TableDataInfo<Reservation>> pageQueryReservation(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize, @RequestParam(required = false) Integer status) {

        TableDataInfo<Reservation> tableDataInfo = reservationService.pageQueryReservation(pageNum, pageSize, status);

        return R.ok(tableDataInfo);
    }

    /**
     * 取消预约
     *
     * @param id
     * @return
     */
    @PutMapping("/{id}/cancel")
    public AjaxResult cancelReservation(@PathVariable Long id) {
        return toAjax(reservationService.cancelReservation(id));
    }
}
